package com.vitas.coin.ui.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.vitas.coin.data.bean.ChildrenVoiceBean;
import com.vitas.coin.databinding.DlgListBinding;
import com.vitas.coin.ui.adapter.ListAdapter;

import java.util.List;

public class ListDlg extends BottomDlg {
    DlgListBinding binding;
    List<ChildrenVoiceBean> list;
    int now;
    ChangeListener listener;
    ListAdapter adapter;

    public ListDlg setParam(List<ChildrenVoiceBean> list, int i, ChangeListener listener) {
        this.list = list;
        this.now = i;
        this.listener = listener;
        return this;
    }

    public void updateNow(int i) {
        if (adapter != null) {
            adapter.updateNow(i);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DlgListBinding.inflate(inflater, container, false);
        binding.close.setOnClickListener(v -> dismiss());
        binding.list.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        adapter = new ListAdapter(this, now, list);
        binding.list.setAdapter(adapter);

        return binding.getRoot();
    }

    public interface ChangeListener {
        void onRemove(int index);

        void onPlayChange(int index);
    }

    public boolean remove(int i) {
        list.remove(i);
        adapter.notifyItemRemoved(i);
        listener.onRemove(i);
        return true;
    }

    public void play(int i) {
        listener.onPlayChange(i);
    }
}
